package ljye.com.ljye.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.Window;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;

import ljye.com.ljye.R;
import ljye.com.ljye.fragment.Fragment_circle;
import ljye.com.ljye.fragment.Fragment_home;
import ljye.com.ljye.fragment.Fragment_me;
import ljye.com.ljye.fragment.Fragment_parade;
import ljye.com.ljye.utils.Utils;

public class MainActivity extends FragmentActivity {

    private RadioGroup rg_tab;
    private List<Fragment> mFragment;
    //缓存Fragment
    private List<Fragment> cacheFragment = new ArrayList<>();
    //上一个fargment
    private Fragment upfragment;
    private RadioGroup.OnCheckedChangeListener onCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            //找到被点击的RadioButton
            View id = group.findViewById(checkedId);
            //被点击的孩子的id
            int i = group.indexOfChild(id);
            //替换Fragment
            Fragment fragment = mFragment.get(i);
            addShowAndHideFragment(fragment);
//            replaceFragment(fragment);
//            if (index!=3){
//                addShowAndHideFragment(fragment);
//            }else{
//                //判断 是否登陆过 如果登陆 就进去  没有登陆就跳转登陆界面
////                if (NetConstants.IS_LOGINED){
//                Fragment2 fragment3 = mFragment.get(3);
//                addShowAndHideFragment(fragment3);
////                }else {
////                    startActivity(new Intent(MainActivity.this,LoginActivity.class));
////                }
//            }

        }
    };

    //替换fragment
    private void replaceFragment(Fragment fragment) {
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_content, fragment).commit();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Utils.setColor(MainActivity.this, Color.parseColor("#FF8D59"));//橙色
        rg_tab = (RadioGroup) findViewById(R.id.rg_tab);
        initFragment(); //初始化fragment
        rg_tab.setOnCheckedChangeListener(onCheckedChangeListener);
//        ((RadioButton)rg_tab.getChildAt(0)).setChecked(true);
        onCheckedChangeListener.onCheckedChanged(rg_tab, R.id.rb_home);
    }

    private void initFragment() {
        mFragment = new ArrayList<Fragment>();
        mFragment.add(new Fragment_home());
        mFragment.add(new Fragment_parade());
        mFragment.add(new Fragment_circle());
        mFragment.add(new Fragment_me());
    }

    //先添加再隐藏 创建后不销毁
    private void addShowAndHideFragment(Fragment fragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        //如果集合里面不包含这个fragment 就添加并且保存起来
        if (!cacheFragment.contains(fragment)) {
            //main_framelayout 替换哪个占位  换上fragment
            fragmentTransaction.add(R.id.fragment_content, fragment);
            //保存到集合
            cacheFragment.add(fragment);
        } else {
            fragmentTransaction.show(fragment);
        }
        //隐藏上一个fragment
        if (upfragment != null) {
            fragmentTransaction.hide(upfragment);
        }
        fragmentTransaction.commit();
        upfragment = fragment;
    }
}